package com.vhans.core.service.impl;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexRequest;
import com.vhans.core.service.ElasticsearchService;
import com.vhans.core.web.model.vo.QuizSearchVO;
import com.vhans.core.web.model.vo.RecordSearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

import static com.vhans.core.constant.ElasticConstant.QUIZ_INDEX;
import static com.vhans.core.constant.ElasticConstant.RECORD_INDEX;

/**
 * es文本业务接口实现类
 *
 * @author vhans
 **/
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {

    @Autowired
    private ElasticsearchClient elasticsearchClient;

    @Override
    public void addRecord(RecordSearchVO record) {
        try {
            // 创建一个 IndexRequest 对象，该对象包含了要添加到索引中的记录的信息，包括索引名称、记录ID 和记录内容
            IndexRequest<RecordSearchVO> indexRequest = IndexRequest.of(request -> request
                    .index(RECORD_INDEX)
                    .id(record.getId().toString())
                    .document(record));
            // 将记录添加到指定的索引中
            elasticsearchClient.index(indexRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateRecord(RecordSearchVO record) {
        try {
            elasticsearchClient.update(request -> request
                    .index(RECORD_INDEX)
                    .id(record.getId().toString())
                    .doc(record), RecordSearchVO.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteRecord(Integer id) {
        try {
            elasticsearchClient.delete(
                    deleteRequest -> deleteRequest
                            .index(RECORD_INDEX)
                            .id(id.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addQuiz(QuizSearchVO quiz) {
        try {
            IndexRequest<QuizSearchVO> indexRequest = IndexRequest.of(request -> request
                    .index(QUIZ_INDEX)
                    .id(quiz.getId().toString())
                    .document(quiz));
            elasticsearchClient.index(indexRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateQuiz(QuizSearchVO quiz) {
        try {
            elasticsearchClient.update(request -> request
                    .index(QUIZ_INDEX)
                    .id(quiz.getId().toString())
                    .doc(quiz), RecordSearchVO.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteQuiz(Integer id) {
        try {
            elasticsearchClient.delete(
                    deleteRequest -> deleteRequest
                            .index(QUIZ_INDEX)
                            .id(id.toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}